package com.example.carbonbackend.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.carbonbackend.constant.CommonConstant;
import com.example.carbonbackend.mapper.MassageMapper;
import com.example.carbonbackend.model.dto.massage.MassageQueryRequest;
import com.example.carbonbackend.model.entity.Massage;
import com.example.carbonbackend.model.vo.MassageVO;
import com.example.carbonbackend.service.MassageService;
import com.example.carbonbackend.utils.SqlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 消息表(Massage)表服务实现类
 *
 * @author makejava
 * @since 2024-03-27 21:43:31
 */
@Service("massageService")
public class MassageServiceImpl extends ServiceImpl<MassageMapper, Massage> implements MassageService {

    @Override
    public QueryWrapper<Massage> getQueryWrapper(MassageQueryRequest massageQueryRequest) {
        QueryWrapper<Massage> queryWrapper = new QueryWrapper<>();
        if (massageQueryRequest == null) {
            return queryWrapper;
        }
        Long id = massageQueryRequest.getId();
        Integer isRead = massageQueryRequest.getIsRead();
        Long senderId = massageQueryRequest.getSenderId();
        Long receiverId = massageQueryRequest.getReceiverId();
        String sortField = massageQueryRequest.getSortField();
        String sortOrder = massageQueryRequest.getSortOrder();

        // 拼接查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(isRead), "is_read", isRead);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(senderId), "sender_id", senderId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(receiverId), "receiver_id", receiverId);
        queryWrapper.eq("is_delete", false);

        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    @Override
    public MassageVO getMassageVO(Massage massage, HttpServletRequest request) {
        if (massage == null) {
            return null;
        }

        MassageVO massageVO = new MassageVO();
        BeanUtils.copyProperties(massage, massageVO);

        return massageVO;
    }

    @Override
    public Page<MassageVO> getMassageVOPage(Page<Massage> massagePage, HttpServletRequest request) {
        List<Massage> massageList = massagePage.getRecords();
        Page<MassageVO> massageVOPage = new Page<>(massagePage.getCurrent(), massagePage.getSize(), massagePage.getTotal());

        if (CollectionUtils.isEmpty(massageList)) {
            return massageVOPage;
        }

        // 2.筛选出用户本身的交易记录
        List<MassageVO> collect = massageList.stream().map(massage -> {

            MassageVO massageVO = new MassageVO();

            BeanUtils.copyProperties(massage,massageVO);

           return massageVO;

        }).collect(Collectors.toList());

        massageVOPage.setRecords(collect);

        return massageVOPage;
    }


}
